package com.guodi.model.utils;

import com.guodi.core.minio.props.MinioProperties;
import com.guodi.core.minio.service.IMinioService;
import com.guodi.core.tool.utils.Charsets;
import com.guodi.core.tool.utils.DateUtil;
import com.guodi.core.tool.utils.StringUtil;
import io.minio.MinioClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class MinioZipDownloadUtil implements InitializingBean {
    private static final Logger log = LoggerFactory.getLogger(MinioZipDownloadUtil.class);
    @Resource
    private MinioClient minioClient;
    @Resource
    private MinioProperties minioProperties;
    @Resource
    private IMinioService minioService;
    private static MinioZipDownloadUtil minioZipDownloadUtil;
    public static final boolean SORT = true;
    public static final boolean NOT_SORT = false;
    private static final Integer DEFAULT_EXPIRY = 60;
    public static final Integer UPLOAD_SUCCESS = 0;
    public static final Integer UPLOAD_PART = 1;
    public static final String DEFAULT_ENCODED = "UTF-8";

    public MinioZipDownloadUtil() {
    }


    public static void batchDownload(List<String> fileUrlList,String projectId, String zipName, HttpServletResponse httpServletResponse) {
        ZipOutputStream zos = null;
        ZipEntry zipEntry = null;
        byte[] buff = new byte[1024];
        if (fileUrlList != null && !fileUrlList.isEmpty()) {
            try {
                if (StringUtil.isEmpty(zipName)) {
                    zipName = "批量下载" + DateUtil.format(new Date(), "yyyyMMddHHmmss");
                }

                httpServletResponse.reset();
                httpServletResponse.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(zipName, Charsets.UTF_8.name()) + ".zip");
                httpServletResponse.setContentType("application/x-msdownload");
                httpServletResponse.setCharacterEncoding("utf-8");
                zos = new ZipOutputStream(httpServletResponse.getOutputStream());
                Iterator var6 = fileUrlList.iterator();

                while(var6.hasNext()) {
                    String fileUrl = (String)var6.next();
                    String downloadUrl = fileUrl.substring(fileUrl.indexOf(minioZipDownloadUtil.minioProperties.getBucketName()) + minioZipDownloadUtil.minioProperties.getBucketName().length());
                    InputStream inputStream = minioZipDownloadUtil.minioService.get(downloadUrl);
                    String fileName = fileUrl.substring(fileUrl.lastIndexOf(projectId + "/"), fileUrl.lastIndexOf("."));
                    zipEntry = new ZipEntry(fileName + fileUrl.substring(fileUrl.lastIndexOf(".")));
                    zos.putNextEntry(zipEntry);
                    boolean var11 = false;

                    int length;
                    while((length = inputStream.read(buff)) > 0) {
                        zos.write(buff, 0, length);
                    }
                }

                log.info("批量下载成功!");
                zos.close();
            } catch (IOException var15) {
                var15.printStackTrace();
                log.error("批量下载发生错误! msg=" + var15.getMessage());
            } finally {
                ;
            }
        } else {
            log.error("批量下载发生错误,文件访问路径集合不能为空!");
        }

    }

    @Override
    public void afterPropertiesSet() throws Exception {

    }
}
